package cn.q3c.download.commons;

import java.beans.PropertyVetoException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * download项目配置信息
 * @author wangxinning
 *
 */
public class Configuration {

	public final static Logger logger = Logger.getLogger(Configuration.class);
	
	private static Configuration instance=new Configuration();
	
	public XMLConfiguration config = null;
	
	private Configuration()
	{
		try {
			config = new XMLConfiguration("download.xml");
			
			//此处可以添加监听器，实现动态更新配置文件内容
			config.setReloadingStrategy(new FileChangedReloadingStrategy());
			
		} catch (ConfigurationException e) {
			logger.error("download.xml文件加载失败", e);
		}
	}
	
	public static Configuration getInstance() {
		return instance;
	}

	//获取下载文件本地保存路径
	public String getStorePath()
	{
		String downloadPath = System.getProperty("download.data.path");
		if( StringUtils.isBlank(downloadPath) )
		{
			downloadPath = config.getString("storepath");
		}
					
		return downloadPath;
	}
	
	//waittime
	public int getWaitTime()
	{
		return config.getInt("waittime",10);
	}

	//下载项目列表
	public List<ConfigProject> getConfigProjectList()
	{
		int maxno = config.getMaxIndex("projects.project");
		
		List<ConfigProject> retList = new ArrayList<ConfigProject>();
		for( int i = 0; i <= maxno; i++ )
		{
			ConfigProject configProject = new ConfigProject();
			
			configProject.setDescription(config.getString("projects.project(" + i + ").description"));
			configProject.setMaxfiles(config.getInt("projects.project(" + i + ").maxfiles"));
			configProject.setMaxthread(config.getInt("projects.project(" + i + ").maxthread"));
			configProject.setName(config.getString("projects.project(" + i + ").name"));
			configProject.setSpiderclass(config.getString("projects.project(" + i + ").spiderclass"));
			configProject.setTimelimit(config.getInt("projects.project(" + i + ").timelimit"));
			configProject.setUrls(config.getString("projects.project(" + i + ").urls"));
			configProject.setSaveLimitHour(config.getInt("projects.project(" + i + ").saveLimitHour"));			
			configProject.setFrompageno(config.getInt("projects.project(" + i + ").frompageno",0));
			configProject.setMaxpageno(config.getInt("projects.project(" + i + ").maxpageno",0));
			configProject.setType(config.getString("projects.project(" + i + ").type", "" ));
			configProject.setActive(config.getBoolean("projects.project(" + i + ").active", true ));//默认活动状态
			
			retList.add(configProject);
		}
		
		return retList;
	}
	
	public static void main( String[] args )
	{
		System.out.println( Configuration.getInstance().getConfigProjectList() );
	}
}